package com.entity.chat;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.dao.SqlOfChat;
import com.servlet.My;
import hyl.base.cache2.TimeCache;
import hyl.core.MyFun;
import hyl.ext.web.ms.*;
public class User {

	private static TimeCache<User> _usercache = null;
	public int uid;// 整形编号
	public String sname;
	public String sImg;
	public String s昵称;
	public String s性别;
	public String s签名;
	public String s地区;
	public String s经度坐标;
	public String s纬度坐标;
	public String s电话;
	public String s主页;
	public String sToken; // 临时令牌
	// 不序列化
	public transient Map<String, Contact> _联系人 = new ConcurrentHashMap<>();

	public Contact getContact(int 类型, int friend) {
		return _联系人.get(类型 + "_" + friend);
	}

	public Collection<Contact> getContacts() {
		return _联系人.values();
	}

	public User(Map<String, Object> mp) {
		this.uid = MyFun.obj2Int(mp.get("id"));
		String sid = String.valueOf(this.uid);
		this.sImg = MyFun.obj2Str(mp.get("headimg"));
		this.s性别 = MyFun.obj2Str(mp.get("sex"));
		this.s昵称 = MyFun.obj2Str(mp.get("ualias"));
		_usercache.put(sid, this);

	}

	public static User select会员(int id) {
		String sid = "0_" + id;
		if (_usercache == null)
			_usercache = TimeCache.getInstance("chatcache");
		User u = null;
		if (_usercache.containsKey(sid))
			u = _usercache.get(sid);
		else
			u = new User(TMsB.getInstance().getUserByID(id)); // 我是谁
		u.addContacts();
		return u;

	}

	public void addContact(int 类型, int friend) {
		if (_联系人.containsKey(类型 + "_" + friend))
			return;
		Map<String, Object> mp = SqlOfChat.get聊天对象(My.getDB1(), uid, friend);
		Contact ct = Contact.newContact(mp);
		_联系人.put(ct.sid, ct);
	}

	public void addContacts() {
		_联系人.clear();
		List<Map<String, Object>> li = SqlOfChat.get我的联系人(uid, null);// 我的好友是谁
		for (Map<String, Object> mp : li) {
			Contact ct = Contact.newContact(mp);
			_联系人.put(ct.sid, ct);
		}
	}

}
